package com.jz.community.basecomm.token;

import android.content.Context;

import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.basecomm.utils.Preconditions;

import okhttp3.Request;

public class TokenUtil {

    private static TokenUtil tokenUtil = null;

    public static TokenUtil getInstance() {
        if (tokenUtil == null){
            tokenUtil = new TokenUtil();
        }
        return tokenUtil;
    }

    /**
     * 为请求加入access_Token以验证用户身份
     */
    public static Request.Builder addUserTokenIfPossible(Context applicationCtx, Request.Builder builder) {
        //加入身份验证信息
        if (applicationCtx != null) {
            String access_Token = BaseSpUtils.getInstance().getAccess_token(applicationCtx);
            if (!Preconditions.isNullOrEmpty(access_Token)) {
                LoggerUtils.fLog().i("access_Token________________" + access_Token);
            }
            if (!Preconditions.isNullOrEmpty(access_Token)) {
                builder.header("Authorization", "Bearer " + access_Token);
            }
        }
        return builder;
    }
}
